﻿Imports Persistencia
Public Class ctrOcorrencias
    Inherits ctrPadrao
#Region "Varíaveis"

    Private objOcorrencias As New perOcorrencias
    Private objClientes As New ctrCliente
    Private prvIDOcorrencia As Int32


#End Region

#Region "Propriedades"
    Public Property IDOcorrencia()
        Get
            Return prvIDOcorrencia
        End Get
        Set(ByVal value)
            prvIDOcorrencia = value
        End Set
    End Property
#End Region

#Region "Métodos"
    Public Function RetornaUltimoContato(ByVal iIDCliente As Int32) As Date
        Try
            Return objOcorrencias.RetornaUltimoContato(iIDCliente)

        Catch ex As Exception

            Throw New Exception("Exceção no método 'RetornaUltimoContato'. " & Environment.NewLine & Environment.NewLine & ex.Message)

        End Try

    End Function
    Public Function salvarOcorrencia(ByVal iIDOcorrencia As Int32, ByVal iIDCliente As Int32, ByVal iIDContato As Int32, ByVal iIDUsuario As Int32, _
                                     ByVal iIDStatus As Int32, ByVal sDescricao As String, ByVal dtDataInicio As Date, _
                                     ByVal dtDataFim As Date) As Boolean
        Dim bRetorno As Boolean = False

        If (iIDOcorrencia > 0) Then
            bRetorno = (objOcorrencias.alteraOcorrencia(iIDOcorrencia, iIDCliente, iIDContato, _
                                                        sDescricao, iIDUsuario, dtDataInicio, _
                                                        dtDataFim) > 0)
        Else
            iIDOcorrencia = Me.inserirOcorrencia(iIDCliente, iIDContato, iIDUsuario, iIDStatus, sDescricao, dtDataInicio, _
                                                 dtDataFim)
            Me.prvIDOcorrencia = iIDOcorrencia
            bRetorno = True
        End If

        Return bRetorno

    End Function
    Private Function inserirOcorrencia(ByVal iIDCliente As Int32, ByVal iIDContato As Int32, ByVal iIDUsuario As Int32, _
                                       ByVal iIDStatus As Int32, ByVal sDescricao As String, ByVal dtDataInicio As Date, _
                                       ByVal dtDataFim As Date) As Int32
        Dim iIDOcorrencia As Int32

        Try

            iIDOcorrencia = objOcorrencias.insereOcorrencia(iIDCliente, iIDContato, sDescricao, iIDUsuario, dtDataInicio, _
                                                            dtDataFim)
            If (iIDOcorrencia > 0) Then
                objClientes.AtualizaProximoContato(iIDCliente, iIDStatus, dtDataInicio)
            End If

            Return iIDOcorrencia

        Catch ex As Exception
            Throw New Exception("Exceção no método 'inserirOcorrencia'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function
    Public Function RetornaContatosRealizados(ByVal iIDCliente As Int32, ByVal iIDStatus As Int32, ByVal iIDUsuario As Int32, _
                                               ByVal dtDataInicio As Date, ByVal dtDataFim As Date) As DataTable
        If (dtDataInicio = Date.MinValue) Then
            dtDataInicio = CDate("01/01/1900")
        End If
        If (dtDataFim = Date.MinValue) Then
            dtDataFim = CDate("01/01/2099")
        End If

        Return Me.objOcorrencias.selecionaContatosRealizados(iIDCliente, _
                                                                iIDStatus, _
                                                                iIDUsuario, _
                                                                dtDataInicio, _
                                                                dtDataFim)
    End Function

    Public Function SelecionaOcorrencia(ByVal iIDOcorrencia As Int32) As DataRow

        Return Me.objOcorrencias.selecionaOcorrencia(iIDOcorrencia)

    End Function

#End Region
End Class
